Conditions | 25 |
Paths | 40 |
Total Lines | 101 |
Code Lines | 69 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like index.js ➔ expand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | var concatMap = require('concat-map'); |
||
100 | function expand(str, isTop) { |
||
101 | var expansions = []; |
||
102 | |||
103 | var m = balanced('{', '}', str); |
||
104 | if (!m || /\$$/.test(m.pre)) return [str]; |
||
105 | |||
106 | var isNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/.test(m.body); |
||
107 | var isAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/.test(m.body); |
||
108 | var isSequence = isNumericSequence || isAlphaSequence; |
||
109 | var isOptions = m.body.indexOf(',') >= 0; |
||
110 | if (!isSequence && !isOptions) { |
||
111 | // {a},b} |
||
112 | if (m.post.match(/,.*\}/)) { |
||
113 | str = m.pre + '{' + m.body + escClose + m.post; |
||
114 | return expand(str); |
||
115 | } |
||
116 | return [str]; |
||
117 | } |
||
118 | |||
119 | var n; |
||
120 | if (isSequence) { |
||
121 | n = m.body.split(/\.\./); |
||
122 | } else { |
||
123 | n = parseCommaParts(m.body); |
||
124 | if (n.length === 1) { |
||
125 | // x{{a,b}}y ==> x{a}y x{b}y |
||
126 | n = expand(n[0], false).map(embrace); |
||
127 | if (n.length === 1) { |
||
128 | var post = m.post.length |
||
129 | ? expand(m.post, false) |
||
130 | : ['']; |
||
131 | return post.map(function(p) { |
||
132 | return m.pre + n[0] + p; |
||
133 | }); |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | |||
138 | // at this point, n is the parts, and we know it's not a comma set |
||
139 | // with a single entry. |
||
140 | |||
141 | // no need to expand pre, since it is guaranteed to be free of brace-sets |
||
142 | var pre = m.pre; |
||
143 | var post = m.post.length |
||
144 | ? expand(m.post, false) |
||
145 | : ['']; |
||
146 | |||
147 | var N; |
||
148 | |||
149 | if (isSequence) { |
||
150 | var x = numeric(n[0]); |
||
151 | var y = numeric(n[1]); |
||
152 | var width = Math.max(n[0].length, n[1].length) |
||
153 | var incr = n.length == 3 |
||
154 | ? Math.abs(numeric(n[2])) |
||
155 | : 1; |
||
156 | var test = lte; |
||
157 | var reverse = y < x; |
||
158 | if (reverse) { |
||
159 | incr *= -1; |
||
160 | test = gte; |
||
161 | } |
||
162 | var pad = n.some(isPadded); |
||
163 | |||
164 | N = []; |
||
165 | |||
166 | for (var i = x; test(i, y); i += incr) { |
||
167 | var c; |
||
168 | if (isAlphaSequence) { |
||
169 | c = String.fromCharCode(i); |
||
170 | if (c === '\\') |
||
171 | c = ''; |
||
172 | } else { |
||
173 | c = String(i); |
||
174 | if (pad) { |
||
175 | var need = width - c.length; |
||
176 | if (need > 0) { |
||
177 | var z = new Array(need + 1).join('0'); |
||
178 | if (i < 0) |
||
179 | c = '-' + z + c.slice(1); |
||
180 | else |
||
181 | c = z + c; |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | N.push(c); |
||
186 | } |
||
187 | } else { |
||
188 | N = concatMap(n, function(el) { return expand(el, false) }); |
||
189 | } |
||
190 | |||
191 | for (var j = 0; j < N.length; j++) { |
||
192 | for (var k = 0; k < post.length; k++) { |
||
193 | var expansion = pre + N[j] + post[k]; |
||
194 | if (!isTop || isSequence || expansion) |
||
195 | expansions.push(expansion); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | return expansions; |
||
200 | } |
||
201 | |||
202 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.